$(function() {
  var tableView = null;
  var isInited = false;
  var dataSource = [];
  var theKey = 0;
  $('.owl-carousel').owlCarousel({
    loop: false,
    margin: 1,
    lazyLoad: true,
    nav: false,
    items: 1,
  });
  setTimeout(function() {
    theKey = 10;
    dataSource = [];
    for (var i = 0; i < theKey; i++) {
      dataSource.push({
        namestr: '第' + i + '个item'
      })
    }
    refresTableView(false);
  }, 2000);
  function refresTableView(isClearAll) {
    if (tableView) {
      tableView.notifyDatasetChage(isClearAll);
    } else {
      $(".real_listview").iscrollListView({}, {
        onPullUp: function(theScroller) {
          setTimeout(function() {
            var startIndex = theKey;
            theKey += 10;
            for (var i = startIndex; i < theKey; i++) {
              dataSource.push({
                namestr: '第' + i + '个item'
              })
            }
            theScroller.notifyDatasetChage(false);
          }, 2000);
        },
        needPullUpFooter: function() {
          return {
            height: 50,
            dom: $('<div class="pullUp"><span class="pullUpLabel">上拉加载更多</span></div>').on('statuschanged', function(event, status) {
              switch (status.status) {
                case 'LSinitF':
                  $(this).find('.pullUpLabel').html('上拉加载更多');
                  break;
                case 'LSLoadingF':
                  $(this).find('.pullUpLabel').html('加载中');
                  break;
                case 'LSflipF':
                  $(this).find('.pullUpLabel').html('松开加载更多');
                  break;
                default:
              }
            })[0]
          };
        },
        onPullDown: function(theScroller) {
          setTimeout(function() {
            theKey = 10;
            dataSource = [];
            for (var i = 0; i < theKey; i++) {
              dataSource.push({
                namestr: '第' + i + '个item'
              })
            }
            theScroller.notifyDatasetChage(false);
          }, 2000);
        },
        needPullDownHeader: function() {
          return {
            height: 50,
            dom: $('<div class="pullUp"><span class="pullUpLabel">下拉加载更多</span></div>').on('statuschanged', function(event, status) {
              switch (status.status) {
                case 'LSinitF':
                  $(this).find('.pullUpLabel').html('xia拉加载更多');
                  break;
                case 'LSLoadingF':
                  $(this).find('.pullUpLabel').html('加载中');
                  break;
                case 'LSflipF':
                  $(this).find('.pullUpLabel').html('松开shuaxin');
                  break;
                default:
              }
            })[0]
          };
        },
        needDataCount: function() {
          return dataSource.length;
        },
        needLineHeight: function() {
          return 60;
        },
        onInitFinish: function(scroller) {
          tableView = scroller;
        },
        hasMoreData: function() {
          return true;
        },
        configItem: function(index) {
          return $($.nano($("#list_item_template").val(), dataSource[index]));
        }
      });
      document.addEventListener('touchmove', function(e) {
        e.preventDefault();
      }, false);
    }
  }
});
